package cn.edu.nuc.zyb.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.edu.nuc.zyb.client.UserClient;
import cn.edu.nuc.zyb.model.Cart;

import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserCartFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	float money = 0;
	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { UserCartFrame frame = new
	 * UserCartFrame(); frame.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */

	/**
	 * Create the frame.
	 */
	public UserCartFrame(List<Cart> list, String name) {
		setTitle("购物车详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 591, 377);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(￥)");
		model.addColumn("数量");
		model.addColumn("总价格(￥)");

		// model.addColumn("操作");
		for (int i = 0; i < list.size(); i++) {
			Cart cart = list.get(i);
			String id = String.valueOf(cart.getId());
			String price = String.valueOf(cart.getPrice());
			String numbers = String.valueOf(cart.getNum());
			String allmoney = String.valueOf(cart.getNum() * cart.getPrice());
			model.addRow(new String[] { id, cart.getName(), price, numbers, allmoney });
			money = (cart.getNum() * cart.getPrice()) + money;
		}

		table = new JTable(model);
		table.setBounds(10, 38, 543, 184);

		// JScrollPane pane = new JScrollPane( table );

		contentPane.add(table);

		JLabel label = new JLabel("总商品数量: " + list.size());
		label.setBounds(195, 232, 116, 15);
		contentPane.add(label);

		JLabel lblNewLabel = new JLabel("总金额: " + money);
		lblNewLabel.setBounds(342, 232, 211, 15);
		contentPane.add(lblNewLabel);

		JButton button = new JButton("结账");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserClient uc = new UserClient();
				uc.settleAccounts(name, money);
				if (uc.acceptMsg()) {
					JOptionPane.showMessageDialog(null, "付款成功");
					UserCartFrame.this.setVisible(false);
				} else {
					JOptionPane.showMessageDialog(null, "付款失败");
				}
			}
		});
		button.setBounds(456, 283, 93, 23);
		contentPane.add(button);

	}

}
